package com.ygxsk.carhome.utils;

//import static android.provider.Settings.EXTRA_APP_PACKAGE;

import static android.provider.Settings.EXTRA_APP_PACKAGE;
import static androidx.core.app.NotificationCompat.EXTRA_CHANNEL_ID;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;

import com.blankj.utilcode.util.PermissionUtils;
import com.ygxsk.carhome.YourNotificationListenerService;

/*
 * @Author:itchenqi175@163.com
 * @Time:2024/1/30  13:46
 * @Description:判断用户是否开启通知
 */
public class MyNotificationUtils {

    // 检查通知访问权限是否已授权
    public static boolean isNotificationListenerEnabled(Context context) {

        ComponentName cn = new ComponentName(context, YourNotificationListenerService.class);
        String flat = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");

        return flat != null && flat.contains(cn.flattenToString());
    }

    // 启动系统设置界面，让用户手动开启通知监听服务
    public static void requestNotificationListenerPermission(Context context) {

        try {

            //do
            //一个拆分的写法
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
//            intent.putExtra("app_package", applicationInfo.packageName);
//            intent.putExtra("android.provider.extra.APP_PACKAGE", applicationInfo.packageName);
//            intent.putExtra("app_uid", applicationInfo.uid);

            Intent intent;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
            } else {
                intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            }

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            // 根据isOpened结果，判断是否需要提醒用户跳转AppInfo页面，去打开App通知权限
//            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);

            //这种方案适用于 API 26, 即8.0（含8.0）以上可以用
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                intent.putExtra(EXTRA_APP_PACKAGE, context.getPackageName());
                intent.putExtra(EXTRA_CHANNEL_ID, context.getApplicationInfo().uid);

            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {

                intent.putExtra("app_package", context.getPackageName());
                intent.putExtra("app_uid", context.getApplicationInfo().uid);

            } else {

                // TODO: 2024/1/31 10:33 暂存不知道是不是低版本也这样写法？
                intent.putExtra("app_package", context.getPackageName());
                intent.putExtra("app_uid", context.getApplicationInfo().uid);

            }

            context.startActivity(intent);

        } catch (Exception e) {
            //catch
            e.printStackTrace();

            //如果设备打不开就
            //打开系统的权限设置
            PermissionUtils.launchAppDetailsSettings();

        }

    }

    // 启动系统设置界面，让用户手动开启通知监听服务
//    public static void requestNotificationListenerPermission(Context context) {
//        Intent intent;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
//            intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
//        } else {
//            intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
//        }
//        context.startActivity(intent);
//    }

}
